<template>
    <div class="addOrEdit">
        <el-dialog
            :before-close="handClose"
            center
            width="30%"
            title="菜单添加或修改"
            :visible.sync="off"
            append-to-body>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" size="mini" class="demo-ruleForm">
                <el-form-item label="菜单名" prop="menuName">
                    <el-input v-model.trim="ruleForm.menuName"></el-input>
                </el-form-item>
                 <el-form-item label="菜单地址" prop="url">
                    <el-input v-model.trim="ruleForm.url"></el-input>
                </el-form-item>
                 <el-form-item label="菜单序号" prop="sortNumber">
                    <el-input v-model.trim="ruleForm.sortNumber"></el-input>
                </el-form-item>
                 <el-form-item label="菜单图标" prop="menuIcon">
                    <el-input v-model.trim="ruleForm.menuIcon"></el-input>
                </el-form-item>
                 <el-form-item label="菜单类型" prop="type">
                    <el-input v-model.trim="ruleForm.type"></el-input>
                </el-form-item>
                <el-form-item label="菜单父ID" prop="parentId">
                    <el-input v-model.trim="ruleForm.parentId"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="create">立即创建</el-button>
            </div>
        </el-dialog>
    </div>
   
</template>

<script>
    import { addMenu, editMenu } from "@/api/menu.js"
    export default {
        name: "addOrEdit",
        props: {
            off: {
                type: Boolean
            },
            rowData: {
                type: Object
            },
            type: {
                type: String
            },
        },
        watch: {
            off(val){
                if(val){
                    if(this.type == "add"){
                        this.$nextTick(() => {
                            this.$refs.ruleForm.resetFields();
                        });
                    }
                    if(this.type == "edit"){
                        this.ruleForm.menuName = this.rowData.menuName;
                        this.ruleForm.url = this.rowData.url;
                        this.ruleForm.sortNumber = this.rowData.sortNumber;
                        this.ruleForm.menuIcon = this.rowData.menuIcon;
                        this.ruleForm.type = this.rowData.type;
                        this.ruleForm.parentId = this.rowData.parentId;
                    }
                }
            }
        },
        data(){
            return {
                rules: {
                     menuName: [{ required: true, message: "菜单名称不能为空", trigger: "blur" }],
                     url: [{ required: true, message: "vue上面的url地址", trigger: "blur" }],
                     sortNumber: [{ required: true, message: "菜单排序序号", trigger: "blur" }],
                     menuIcon: [{ required: true, message: "菜单图标不能为空", trigger: "blur" }],
                     type: [{ required: true, message: "类型: 0代表菜单,1代表按钮", trigger: "blur" }],
                     parentId: [{ required: true, message: "父级菜单的ID", trigger: "blur" }]
                },
                ruleForm: {
                    menuName: "",
                    url: "",
                    sortNumber: "",
                    menuIcon: "",
                    type: "",
                    parentId: "",
                }
            }
        },
        methods: {
            handClose(){
                this.$emit("update:off", false);
            },
            create(){
                this.$refs.ruleForm.validate((valid) => {
                    if(!valid){
                        return;
                    }else{
                        if(this.type == "edit"){
                            editMenu(this.ruleForm).then(
                                res => {
                                    this.$message({
                                        type: "success",
                                        message: "修改成功"
                                    });
                                    this.$emit("update:off", false);
                                    this.$parent.getList();
                                    this.$refs.ruleForm.resetFields();
                                },
                                err => {}
                            );
                        }else{
                            addMenu(this.ruleForm).then(
                                res => {
                                    console.log("res新增菜单", res);
                                    this.$message({
                                        type: "success",
                                        message: "新增菜单成功"
                                    });
                                    this.$emit("update:off", false);
                                    this.$parent.getList();
                                    this.$refs.ruleForm.resetFields();
                                },
                                err => {}
                            );
                        }
                    }
                });
            }
        }
    }
</script>

<style scoped lang="less">

</style>